package com.javaweb.service.dto;

import com.javaweb.common.StringUtils;
import com.javaweb.model.Article;
import com.javaweb.model.Tag;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.stream.Collectors;

/**
 * @desc:用以显示在文章管理页面中的 Article信息。
 * @author: Ping
 * @date: 2024/10/17 9:39
 */

@Data
@Setter
@Getter
@Accessors(chain = true)
public class ArticleDto implements Serializable {
    private static final long serialVersionUID = 1L;
    private String id ;
    private String title ;
    private String content ;
    private String authorName ;
    private String categoryName ;
    /**
     * tag names str, seperated by ','
     */
    private String tags ;
    private boolean recommended ;
    private boolean isTop ;
    private String status ;
    private String createTime ;
    private String updateTime ;
    // 为了简化，删除该属性。
//    private String commentCount ;
    private String viewCount ;
    private String suppCount ;

    /**
     * 将 Article 实体类转换为 ArticleDto, 注意，用户的名字没有设置。
     * @param article
     * @return
     */
    public static ArticleDto fromEntity(Article article){
        ArticleDto articleDto = new ArticleDto();
        articleDto.setId(article.getId().toString());
        articleDto.setTitle(article.getTitle());
        articleDto.setContent(article.getContent());
        if (! ObjectUtils.isEmpty(article.getCategory())) {
            articleDto.setCategoryName(article.getCategory().getName()) ;
        }
        if (! ObjectUtils.isEmpty(article.getTags())) {
            articleDto.setTags(article.getTags().stream().map(Tag::getName).collect(Collectors.joining(",")));
        }
        articleDto.setRecommended(article.getRecommended());
        articleDto.setTop(article.getIsTop());
        articleDto.setStatus(article.getStatus().toString());
        articleDto.setCreateTime(StringUtils.formatDate(article.getCreateTime()));
        articleDto.setUpdateTime(StringUtils.formatDate(article.getUpdateTime()));
        articleDto.setViewCount(String.valueOf(article.getSupportCount() + article.getOpposeCount()));
        articleDto.setSuppCount(String.valueOf(article.getSupportCount()));


        return articleDto ;



    }

}

